const pool = require("../db/index");

const getUsersByUser = async ({ username }) => {
  const sql = "select * from user where username = ?";
  try {
    return await pool.exec(sql, [username]);
  } catch (error) {
    return error;
  }
};

const getUsersByUserAndPwd = async ({ username, password }) => {
  const sql = "select * from user where username = ? and password = ?";
  try {
    return await pool.exec(sql, [username, password]);
  } catch (error) {
    return error;
  }
};

const addUser = async ({ username, password, nickname, avatar }) => {
  const sql = "INSERT INTO `user` (`id`, `username`, `password`, `nickname`, `avatar`) VALUES (NULL, ?, ?, ?, ?);";
  try {
    return await pool.exec(sql, [username, password, nickname, avatar]);
  } catch (error) {
    return error;
  }
};

module.exports = {
  getUsersByUserAndPwd,
  addUser,
  getUsersByUser
}